package com.lcy.web.model.sql.base.mine;

import com.lcy.web.utils.CommonUtils;

/**
 * Created by loucunyin on 2016/12/7 0007.
 * 用于组装简单常用的 MySql 语句，复杂的 SQL 语句使用 xml 获取
 */

public class MySqlTemplate implements SqlTemplate {
    protected String mTableName;

    public MySqlTemplate(String tableName) {
        mTableName = tableName;
    }

    @Override
    public String selectAll() {
        return String.format(SELECT_ALL, mTableName);
    }

    @Override
    public String selectColumns(String[] columns) {
        if (!CommonUtils.checkArray(columns)) {
            return selectAll();
        }

        StringBuilder builder = new StringBuilder();
        for (int i = 0; i < columns.length; i++) {
            String column = columns[i];
            if (i > 0) {
                builder.append(",");
            }

            builder.append(String.format(VALUE, column));
        }

        return String.format(SELECT_COLUMNS, builder.toString(), mTableName);
    }

    @Override
    public String selectByColumn(String column, Object value) {
        return selectByColumn(new SelectCondition(column, value));
    }

    @Override
    public String selectByColumn(SelectCondition condition) {
        if(isInvalidCondition(condition)){
            return null;
        }
        SelectCondition[] conditions = {
                condition
        };
        return selectByColumns(conditions);
    }


    @Override
    public String selectByColumns(SelectCondition[] conditions) {
        return selectByColumns(null, conditions);
    }

    @Override
    public String selectByColumns(String[] columns, SelectCondition[] conditions) {
        String select = selectColumns(columns);
        if (!CommonUtils.checkArray(conditions)) {
            return select;
        }

        return select +
                " where " +
                getConditions(conditions);
    }

    @Override
    public String selectPage(String sql, int page, int pageSize) {

        return limit(sql, page * pageSize, (page + 1) * pageSize - 1);
    }

    @Override
    public String limit(String sql, int start, int end) {
        if (!CommonUtils.checkString(sql)) {
            sql = selectAll();
        }
        String limit = "%s limit %d,%d";
        return String.format(limit, sql, start, end);
    }

    @Override
    public String limit(String sql, int size) {
        String limit = "%s limit %d";
        return String.format(limit, sql, size);
    }

    @Override
    public String selectCount() {
        return selectCount("*");
    }

    @Override
    public String selectCount(String column) {
        return String.format(COUNT, column, mTableName);
    }

    @Override
    public String deleteByColumn(String column, Object value) {
        if (!CommonUtils.checkString(column)) {
            return null;
        }
        SelectCondition[] conditions = {
                new SelectCondition(column, value)
        };
        return deleteByColumns(conditions);
    }

    @Override
    public String deleteByColumn(SelectCondition condition) {
        if(isInvalidCondition(condition)){
            return null;
        }
        SelectCondition[] conditions = {condition};
        return deleteByColumns(conditions);
    }

    @Override
    public String deleteByColumns(SelectCondition[] conditions) {
        if(!CommonUtils.checkArray(conditions)){
            return null;
        }
        return "delete from " +
                mTableName +
                " where " +
                getConditions(conditions);
    }

    private String getConditions(SelectCondition[] conditions){
        StringBuilder builder = new StringBuilder();
        for (int i = 0; i < conditions.length; i++) {
            SelectCondition condition = conditions[i];
            if (!isInvalidCondition(condition)) {
                if (i > 0) {
                    builder.append(" ")
                            .append(condition.getLogic())
                            .append(" ");
                }
                builder.append(condition.getColumn())
                        .append(condition.getOperator())
                        .append(String.format(VALUE, condition.getValue()));
            }
        }
        return builder.toString();
    }

    private boolean isInvalidCondition(SelectCondition condition){
        return (condition == null || !condition.isValid());
    }

}
